<?php

use yii\db\Migration;

class m000004_000005_create_card_attr extends Migration
{
    public function safeUp()
    {
        $this->createTable('{{%card_attr}}', [
            'id' => $this->primaryKey(),
            'uid' => $this->integer()->notNull()->comment('用户编号'),
            'name' => $this->string(32)->notNull()->comment('属性名称'),
            'show_in_list' => $this->integer()->notNull()->defaultValue(0)->comment('是否在列表中显示'),
            'type' => $this->integer()->notNull()->comment('类型'),
            'sort' => $this->integer()->notNull()->defaultValue(0)->comment('排序'),
            'options' => $this->text()->comment('备选值'),
            'desc' => $this->text()->comment('描述'),
            'status' => $this->integer()->notNull()->comment('状态'),
        ]);
        $this->createIndex('fk_card_attr_user1_idx', '{{%card_attr}}', ['uid']);
        try {
            $this->addForeignKey('fk_card_attr_user1', '{{%card_attr}}', ['uid'], '{{%user}}', ['id']);
        } catch (Exception $e) {
            Yii::error($e->getMessage());
        }
        $this->batchInsert('{{%key_map}}', ['t', 'k', 'v'], [
            ['card_attr_type', 1, '密码'],
            ['card_attr_type', 2, '单行文本'],
            ['card_attr_type', 3, '多行文本'],
            ['card_attr_type', 4, '富文本'],
            ['card_attr_type', 5, '数字'],
            ['card_attr_type', 6, '邮箱'],
            ['card_attr_type', 7, '链接'],
            ['card_attr_type', 8, '单选'],
            ['card_attr_type', 9, '多选'],
            ['card_attr_type', 10, '日期时间'],
            ['card_attr_type', 11, '日期'],
            ['card_attr_type', 12, '时间'],
            ['card_attr_type', 13, '文件'],
            ['card_attr_type', 14, '图像'],
            ['card_attr_type', 15, '颜色'],
            ['card_attr_status', 1, '正常'],
            ['card_attr_status', 0, '删除'],
        ]);
    }

    public function down()
    {
        $this->delete('{{%key_map}}', ['t' => 'card_attr_status']);
        $this->delete('{{%key_map}}', ['t' => 'card_attr_type']);
        $this->dropTable('{{%card_attr}}');
    }
}
